package com.wanmait.wanmarecruit.webController;

import com.wanmait.wanmarecruit.entity.LoginLog;
import com.wanmait.wanmarecruit.entity.Recruiter;
import com.wanmait.wanmarecruit.service.LoginLogService;
import com.wanmait.wanmarecruit.service.RecruiterService;
import com.wanmait.wanmarecruit.util.CookieUtils;
import com.wanmait.wanmarecruit.util.PasswordUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("web")
public class WebCompanySignInController {

    @Resource
    private RecruiterService recruiterService;
    @Resource
    private LoginLogService loginLogService;

    @PostMapping("/companies/changePassword")
    public String changePassword(Recruiter recruiter){
        recruiterService.changePassword(recruiter);
        return "redirect:/web/companies/companyLogin";
    }

    @RequestMapping("verifyCompanySignUp")
    @ResponseBody
    public String verifyCompanySignUp(String memPhone){
        boolean flag = recruiterService.verifyCompanySignUp(memPhone);
        System.out.println(flag);
        return flag+"";
    }

    @PostMapping("/companies/tryLogin")
    public ModelAndView tryLogin(Recruiter recruiter, String onCook, HttpServletRequest request, HttpServletResponse response)
    {
        ModelAndView modelAndView = new ModelAndView();
        Recruiter recruiters = recruiterService.LoginOrNots(recruiter);
        if (recruiters==null){
            modelAndView.setViewName("redirect:/web/companies/companyLogin?err=1");
        }
        else {
            HttpSession session = request.getSession();
            session.setAttribute("hr",recruiters);
            LoginLog loginLog = new LoginLog();
            loginLog.setUserId(recruiters.getId());
            loginLog.setIp(request.getRemoteAddr());
            loginLogService.save(loginLog);
            if(onCook!=null){
                String randomPass = PasswordUtils.md5(recruiter.getPass());
                String randomPhone = PasswordUtils.randomString(2)+recruiters.getPhone()+PasswordUtils.randomString(2);
                CookieUtils.addCookie("phone",randomPhone,"/",60*24*24*7,response);
                CookieUtils.addCookie("pass",randomPass,"/",60*24*24*7,response);
            }
            String uri = (String)session.getAttribute("uri");
            if (uri!=null)
            {
                modelAndView.setViewName("redirect:"+uri);
            }
            else {
                modelAndView.setViewName("redirect:/web/companies/companyIndex");
            }
        }
        return modelAndView;
    }

    @RequestMapping("companySignIn")
    public String companySignIn(Recruiter recruiter, String onCook, HttpServletRequest request, HttpServletResponse response){
        Recruiter recruiters = recruiterService.companySignIn(recruiter);
        if(recruiters==null){
            return "redirect:/web/companies/companySignIn?err=1";
        }
        HttpSession session = request.getSession();
        session.setAttribute("hr",recruiters);
        LoginLog loginLog = new LoginLog();
        loginLog.setUserId(recruiters.getId());
        loginLog.setIp(request.getRemoteAddr());
        loginLogService.save(loginLog);
        if (onCook!=null)
        {
            String randomPass = PasswordUtils.md5(recruiters.getPass());
            String randomPhone = PasswordUtils.randomString(2)+recruiters.getPhone()+PasswordUtils.randomString(2);
            CookieUtils.addCookie("phone",randomPhone,"/",60*24*24*7,response);
            CookieUtils.addCookie("pass",randomPass,"/",60*24*24*7,response);
        }
        String uri = (String)session.getAttribute("uri");
        if (uri!=null)
        {
            return "redirect:"+uri;
        }
        else {
            return "redirect:/web/companies/companyIndex";
        }
    }
}
